package cn.base.web.auth.user;

import cn.base.Constants;
import cn.base.web.cahe.DefaultCacheService;
import cn.rengy.auth.SysUser;
import cn.rengy.auth.UserContextHolder;
import cn.rengy.auth.UserService;
import cn.rengy.auth.entity.principal.Identity;
import cn.rengy.isql.Dao;
import cn.rengy.isql.DbService;
import cn.rengy.isql.DbSqlValueService;
import cn.rengy.isql.SqlEntity;
import cn.rengy.util.web.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private DefaultCacheService cacheService;
    @Autowired(required = false)
    private Dao dao;
    @Autowired(required = false)
    private DbSqlValueService dbSqlValueService;

    public SysUser getSysUser(Long userId) {
        if(userId!=null) {
            SysUser sysUser = null;
            try {
                sysUser = cacheService.getCache("userCache", userId.toString(), new Callable<SysUser>() {
                    @Override
                    public SysUser call() throws Exception {
                        SqlEntity sqlEntity=dbSqlValueService.getSqlEntityByName("select_user_info");
                        SysUser sysUser=dao.queryForBean(sqlEntity.getSql_value(), SysUser.class, userId);
                        //SysUser sysUser=BeanUtil.copyProperties(sysUserPO, SysUser.class);
                        //查询角色
                        return sysUser;
                    }
                });
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return sysUser;
        }
        return null;
    }

    public SysUser getSysUserAndRole(Long userId) {
        if(userId!=null) {
            SysUser sysUser = getSysUser( userId);
            //查询角色
            List<String> roles=getRoles(userId);
            //sysUser.setRoles();
            return sysUser;
        }
        return null;
    }

    public List<String> getRoles(Long userId){
        if(userId==null){
            return null;
        }
        try {
            return cacheService.getCache(Constants.KEY_UserRole, userId.toString(), new Callable<List<String>>() {
                @Override
                public List<String> call() throws Exception {
                    SqlEntity sqlEntity=dbSqlValueService.getSqlEntityByName("select_roles_by_userid");
                    return dao.querySingleColumnForList(sqlEntity.getSql_value(), String.class,userId);
                }
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 当前用户至少拥有其中一个角色
     * @param roleIdentifiers
     * @return
     */
    @Override
    public boolean hasRoles(Collection<String> roleIdentifiers){
        Identity identity=(Identity) RequestUtil.getPrincipal();
        if(identity==null){
            return false;
        }
        return hasRoles( identity.getUserId(),  roleIdentifiers);
    }
    @Override
    public boolean hasRoles(Long userId, Collection<String> roleIdentifiers) {
        if (roleIdentifiers == null || roleIdentifiers.isEmpty()) {
            log.warn("roleIdentifiers为空");
            return true;
        }
        if(userId==null){
            return false;
        }
        List<String> userRoles=getRoles(userId);
        if(userRoles==null||userRoles.isEmpty()){
            return false;
        }
        for (String roleCode : roleIdentifiers) {
            if(userRoles.contains(roleCode)){
                return true;
            }
        }
        return false;
    }

    /**
     * 当前用户拥有全部角色
     * @param roleIdentifiers
     * @return
     */
    public boolean hasAllRoles(Collection<String> roleIdentifiers) {
        if (roleIdentifiers == null || roleIdentifiers.isEmpty()) {
            log.warn("roleIdentifiers为空");
            return true;
        }
        Long userId= ((Identity) UserContextHolder.get()).getUserId();
        if(userId==null){
            return false;
        }
        List<String> userRoles=getRoles(userId);
        if(userRoles==null||userRoles.isEmpty()){
            return false;
        }
        for (String roleCode : roleIdentifiers) {
            if(!userRoles.contains(roleCode)){
                return false;
            }
        }
        return true;
    }

}
